package com.ruicar.afs.cloud.apply.contract.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.apply.common.entity.*;
import com.ruicar.afs.cloud.apply.contract.condition.LoanAppFormStateCondition;
import com.ruicar.afs.cloud.apply.contract.service.*;
import com.ruicar.afs.cloud.apply.pre.loan.service.*;
import com.ruicar.afs.cloud.common.core.util.IResponse;

import com.ruicar.afs.cloud.parameter.commom.enums.AffiliatedWay;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import static com.ruicar.afs.cloud.apply.common.utils.ApplyConstants.*;

/**
 * @description: app表单状态(放款)
 * @author: mingzhi.li
 * @date: 2020/8/04 10:28
 */
@Slf4j
@RestController
@RequestMapping("/loanAppFormState")
@AllArgsConstructor
public class LoanAppFormStateController {
    private ApplyCarInvoiceService applyCarInvoiceService;
    private ApplyBankCardService applyBankCardService;
    private ApplyInsuranceInfoService applyInsuranceInfoService;
    private final ApplyOrderInfoService applyOrderInfoService;
    private ApplyAffiliatedUnitService applyAffiliatedUnitService;
    private ApplyContractInfoService applyContractInfoService;
    private ApplyAppService applyAppService;




    @RequestMapping(value = "/getLoanAppFormState", method = RequestMethod.POST)
    @ApiOperation(value = "根据申请编号获取app表单是否已经录入")
    public IResponse<Object> getLoanAppFormState(@RequestBody LoanAppFormStateCondition stateCondition) {
        if(StrUtil.isBlank(stateCondition.getApplyNo())){
            return IResponse.fail("申请编号不能为空");
        }
        LoanAppFormStateCondition condition = new LoanAppFormStateCondition();
        //申请编号返回
        condition.setApplyNo(stateCondition.getApplyNo());
        ApplyOrderInfo applyOrderInfo = applyOrderInfoService.getOne(Wrappers.<ApplyOrderInfo>query().lambda().eq(ApplyOrderInfo::getApplyNo, stateCondition.getApplyNo()));
        condition.setBusinessType(applyOrderInfo.getBusinessType());
        //挂靠方式
        condition.setAffiliatedWay(applyOrderInfo.getAffiliatedWay());
        //车辆属性
        condition.setCarNature(applyOrderInfo.getCarNature());
        ApplyContractInfo contractInfo = applyContractInfoService.getOne(Wrappers.<ApplyContractInfo>query().lambda().eq(ApplyContractInfo::getApplyNo, stateCondition.getApplyNo()));
        if(contractInfo !=null){
            //合同号赋值
            condition.setContractNo(contractInfo.getContractNo());
        }else{
            condition.setContractNo("");
        }
        log.info("======================发票区域组装开始========================》");
        condition = applyAppService.getInvoiceTypeArea(condition,applyOrderInfo);
        log.info("======================发票区域组装结束========================》");

        log.info("======================Gps区域组装开始========================》");
        condition = applyAppService.getGpsInfoArea(condition,applyOrderInfo,contractInfo);
        log.info("======================Gps区域组装结束========================》");

        log.info("======================银行卡区域组装开始========================》");
        List<ApplyBankCard> bankCardList = applyBankCardService.list(Wrappers.<ApplyBankCard>query().lambda().eq(ApplyBankCard::getApplyNo, stateCondition.getApplyNo()));
        if(bankCardList !=null && bankCardList.size()>0 && StrUtil.isNotBlank(bankCardList.get(0).getAuthorizeWay())
                && StrUtil.isNotBlank(bankCardList.get(0).getAccountName())){
            condition.setBankCardState(APP_FORM_STATE_ONE);
        }else{
            condition.setBankCardState(APP_FORM_STATE_ZERO);
        }
        log.info("======================银行卡区域组装结束========================》");

        log.info("======================保险区域组装开始========================》");
        List<ApplyInsuranceInfo> insuranceInfoList = applyInsuranceInfoService.list(Wrappers.<ApplyInsuranceInfo>query().lambda().eq(ApplyInsuranceInfo::getApplyNo, stateCondition.getApplyNo()));
        if(insuranceInfoList !=null && insuranceInfoList.size()>=1){
            condition.setInsuranceState(APP_FORM_STATE_ONE);
        }else{
            condition.setInsuranceState(APP_FORM_STATE_ZERO);
        }
        log.info("======================保险区域组装结束========================》");

        log.info("======================挂靠信息组装开始========================》");
        ApplyAffiliatedUnit affiliatedUnit = applyAffiliatedUnitService.getOne(Wrappers.<ApplyAffiliatedUnit>query().lambda().eq(ApplyAffiliatedUnit::getApplyNo, stateCondition.getApplyNo()));
        if(affiliatedUnit !=null && StrUtil.isNotBlank(affiliatedUnit.getSocUniCrtCode())){
            condition.setAffiliatedState(APP_FORM_STATE_ONE);
        }else{
            if(AffiliatedWay.NO.getIndex().equals(applyOrderInfo.getAffiliatedWay())){
                condition.setAffiliatedState(APP_FORM_STATE_ONE);
            }else{
                condition.setAffiliatedState(APP_FORM_STATE_ZERO);
            }
        }
        log.info("======================挂靠信息组装结束========================》");

        log.info("======================附加贷信息组装开始========================》");
        condition = applyAppService.getFinancingItemsArea(condition);
        log.info("======================附件贷信息组装结束========================》");
        log.info("======================是否特殊业务组装开始========================》");
        //暂时默认：否
        condition.setIsSpecialBusiness(APP_FORM_STATE_ZERO);
        log.info("======================是否特殊业务组装结束========================》");

        log.info("======================客户证件有效期、主借人、共借人组装开始========================》");
        condition = applyAppService.getCustInfoArea(condition);
        log.info("======================客户证件有效期、主借人、共借人组装结束========================》");

        log.info("======================其他信息组装开始========================》");
        condition = applyAppService.getOtherInfoArea(condition);
        log.info("======================其他信息组装开始========================》");

        log.info("======================影像附件区域组装开始========================》");
        condition.setBusiNo(condition.getContractNo());
        condition.setBusiType("loanApply");
        condition = applyAppService.getLoanAttachmentsArea(condition,applyOrderInfo);
        log.info("======================影像附件区域组装结束========================》");

        log.info("======================是否已生成合同开始========================》");
        condition.setIsLoanContract(applyAppService.getLoanFileIsComplete(condition));
        log.info("======================是否已生成合同结束========================》");

        log.info("======================是否重出所有合同标识开始========================》");
        if(StrUtil.isBlank(contractInfo.getReappearFlag())){
            condition.setReappearFlag(NO);
        }else{
            condition.setReappearFlag(contractInfo.getReappearFlag());
        }
        log.info("======================是否重出所有合同标识结束========================》");
        return  IResponse.success(condition);
    }
}
